﻿<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>授权</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="layui/css/layui.css" media="all">
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>
    <blockquote class="layui-elem-quote layui-text">
        <img src="favicon64.ico" style="width:28px;height:28px;" />
        产品授权中心
    </blockquote>
    <form class="layui-form" action="" id="database" style="margin:20px">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">数据库IP：</label>
                <div class="layui-input-inline">
                    <input type="text" name="IP" id="IP" autocomplete="off" class="layui-input" lay-verify="required">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">用户名：</label>
                <div class="layui-input-inline">
                    <input type="text" name="username" id="username" autocomplete="off" class="layui-input" lay-verify="required">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">密码：</label>
                <div class="layui-input-inline">
                    <input type="text" name="password" id="password" autocomplete="off" class="layui-input" lay-verify="required">
                </div>
            </div>
            <div class="layui-inline">
                <button type="submit" class="layui-btn" lay-submit="" lay-filter="connect">连接</button>
                <label id="connectResult" style="margin-left:20px;color:green;">*</label>
            </div>

        </div>
    </form>
    <blockquote class="layui-elem-quote layui-text">
        授权文件操作
    </blockquote>
    <div class="layui-upload" style="margin:20px">
        <button type="button" class="layui-btn layui-btn-normal" id="testList">上传授权文件</button>
        <button type="button" class="layui-btn layui-btn-normal" id="delauthor" style="margin-left:20px">删除授权</button>
        <label name="hcode" id="hcode" style="margin-left:20px">当前设备硬件码：</label>
        <div class="layui-upload-list" style="display:none" id="fileResults">
            <table class="layui-table">
                <thead>
                    <tr>
                        <th>文件名</th>
                        <th>大小</th>
                        <th>状态</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody id="demoList"></tbody>
            </table>
        </div>
    </div>
    <blockquote class="layui-elem-quote layui-text">
        授权信息
    </blockquote>
    <div id="test1" class="demo-tree demo-tree-box" style="margin-left:20px"></div>

    <script src="layui/layui.js" charset="utf-8"></script>
    <!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->
    <script>
        layui.use(['form', 'jquery', 'upload', 'tree'], function () {
            var form = layui.form
                , layer = layui.layer, $ = layui.jquery, upload = layui.upload, tree = layui.tree;
            //监听提交
            form.on('submit(connect)', function (data) {
                var loading = layer.load(0, { shade: true });
                $.ajax({
                    url: "/authority/v1/connect",
                    data: data.field,
                    type: "POST",
                   // dataType: "json",
                    //contentType: "application/json"
                    success: function (data) {
                        layer.closeAll('loading');
                        if (data.result) {
                            $("#connectResult").html("连接成功");
                        } else {
                            $("#connectResult").html("连接失败");
                        }
                    },
                    error: function (data) {
                        layer.closeAll('loading');
                        $("#connectResult").html("请求失败");
                    }
                });
                return false;
            });

            $("#delauthor").click(function () {
                layer.confirm('您确定要删除授权吗？', {
                    btn: ['确定', '取消'] //按钮
                }, function () {
                        layer.closeAll();
                      var loading=  layer.load(0, { shade: true });
                        $.ajax({
                            url: "/authority/v1/delauthor",
                            success: function (data) {
                                layer.closeAll('loading');
                                layer.open({
                                    type: 1
                                    , id: 'layerDemook' //防止重复弹出
                                    , content: '<div style="padding: 20px 100px;">授权已经成功删除</div>'
                                });
                            },
                            error: function (data) {
                                layer.closeAll('loading');
                                layer.open({
                                    type: 1
                                    , id: 'layerDemoerror' //防止重复弹出
                                    , content: '<div style="padding: 20px 100px;">授权删除失败</div>'
                                });
                            }
                        });
                }, function () {
                });
            });
            $.ajax({
                url: "/authority/v1/gethcode",
                type: "Post",
                contentType: "application/json",
                success: function (data) {
                    $("#hcode").val(data.result);
                    $.ajax({
                        url: "/authority/v1/getauthor",
                        type: "get",
                        contentType: "application/json",
                        success: function (data) {
                            data2 = data.result;
                            //常规用法
                            tree.render({
                                elem: '#test1' //默认是点击节点可进行收缩
                                , data: data2
                            });
                        },
                        error: function (data) {
                            layer.msg('读取授权信息错误');
                        }
                    });
                },
                error: function (data) {
                    layer.msg('读取硬件码错误', data.result);
                }
            });
            //多文件列表示例
            var demoListView = $('#demoList')
                , uploadListIns = upload.render({
                    elem: '#testList'
                    , url: '/authority/v1/upload' //改成您自己的上传接口
                    , accept: 'file'
                    , multiple: true, exts: 'ldt' //只允许上传ldt
                    , size: 115 //限制文件大小，单位 KB
                    , auto: true
                    , bindAction: '#testListAction'
                    , choose: function (obj) {
                        $("#fileResults").show();
                        var files = this.files = obj.pushFile(); //将每次选择的文件追加到文件队列
                        //读取本地文件
                        obj.preview(function (index, file, result) {
                            var tr = $(['<tr id="upload-' + index + '">'
                                , '<td>' + file.name + '</td>'
                                , '<td>' + (file.size / 1024).toFixed(1) + 'kb</td>'
                                , '<td>等待上传</td>'
                                , '<td>'
                                , '<button class="layui-btn layui-btn-xs demo-reload layui-hide">重传</button>'
                                , '<button class="layui-btn layui-btn-xs layui-btn-danger demo-delete">删除</button>'
                                , '</td>'
                                , '</tr>'].join(''));

                            //单个重传
                            tr.find('.demo-reload').on('click', function () {
                                obj.upload(index, file);
                            });

                            //删除
                            tr.find('.demo-delete').on('click', function () {
                                delete files[index]; //删除对应的文件
                                tr.remove();
                                uploadListIns.config.elem.next()[0].value = ''; //清空 input file 值，以免删除后出现同名文件不可选
                            });

                            demoListView.append(tr);
                        });
                    }
                    , done: function (res, index, upload) {
                        var tr = demoListView.find('tr#upload-' + index)
                            , tds = tr.children();
                        if (res.result == "") { //上传成功
                            tds.eq(2).html('<span style="color: #5FB878;">上传成功</span>');
                            tds.eq(3).html(''); //清空操作
                            return delete this.files[index]; //删除文件队列已经上传成功的文件
                        } else {
                            tds.eq(2).html('<span style="color: #5FB878;">' + res.result + '</span>');
                        }
                        this.error(index, upload);
                    }
                    , error: function (index, upload) {
                        var tr = demoListView.find('tr#upload-' + index)
                            , tds = tr.children();
                        //tds.eq(2).html('<span style="color: #FF5722;">上传失败</span>');
                        tds.eq(3).find('.demo-reload').removeClass('layui-hide'); //显示重传
                    }
                });

        });
    </script>
</body>
</html>
